﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using WSIMCommons;
using System.Diagnostics;
using System.Security.Permissions;
using WSIMServices.model;
using System.Data;

namespace WSIMServices
{
    // NOTE: If you change the class name "WSIMProfileOperations" here, you must also update the reference to "WSIMProfileOperations" in Web.config.
    public class WSIMProfileOperations : IWSIMProfileOperations
    {

        #region Registration

        [OperationBehavior(Impersonation=ImpersonationOption.Allowed)]
        [PrincipalPermission(SecurityAction.Demand,Role="Guest")]
        public string RegisterUser(RegisterInfo info)
        {
            WSIMProfilesTableAdapters.wsim_userTableAdapter u = ObjectFactory.GetUserTable();
            WSIMProfilesTableAdapters.wsim_user_detailTableAdapter det = ObjectFactory.GetUserDetailTable();
            long uid = DateTime.Now.Ticks;

            if (!info.Validate()) return ErrorCodesEnum.W1000_ERROR_IN_FIELDS.ToString();
            bool half = false;
            try
            {
                u.Insert(uid, info.UserName, info.Password, (byte)UserStatusEnum.ACTIVE, null, info.Nick, DateTime.Now);
                half = true;
                det.Insert(uid, info.Name, info.Surname, info.BirthDate, info.Country, info.Town, info.Email, (byte)(info.Privacy ? 1 : 0), info.Province);
            }
            catch (Exception e)
            {                
                if (half)
                {
                    u.Delete(uid);
                }
                return ErrorCodesEnum.W1001_ERROR_IN_DATABASE_UPDATE.ToString();
            }
            return ErrorCodesEnum.W2000_REGISTRATION_SUCCESSFUL.ToString();
        }

        #endregion

        #region Profile Operations


        public string AddProfile(Profile profile)
        {
            throw new NotImplementedException();
        }

        public string DeleteProfile(Profile profile)
        {
            
            throw new NotImplementedException();
        }

        public List<Profile> GetProfiles()
        {
            throw new NotImplementedException();
        }

        public List<Provider> GetProviders()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region Contacts Operations

        [OperationBehavior(Impersonation = ImpersonationOption.Allowed)]
        [PrincipalPermission(SecurityAction.Demand, Role = "User")]
        public UserList GenerateUserList()
        {
            WSIMProfilesTableAdapters.wsim_user_listTableAdapter list = new WSIMServices.WSIMProfilesTableAdapters.wsim_user_listTableAdapter();
            WSIMProfilesTableAdapters.wsim_userTableAdapter users = new WSIMServices.WSIMProfilesTableAdapters.wsim_userTableAdapter();
            WSIMProfilesTableAdapters.wsim_online_usersTableAdapter online = new WSIMServices.WSIMProfilesTableAdapters.wsim_online_usersTableAdapter();
            string username = OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name;
            WSIMProfiles.wsim_user_listDataTable table = list.GetDataByOwner(username);

            UserList lst = new UserList();
            foreach (DataRow row in table.Rows)
            {
                User u = new User();
                u.UserID = (long)row[2];
                u.Group = row[1].ToString();
                DataRow userFound = users.GetDataByID(u.UserID).Rows[0];
                u.Nickname = userFound["ALIAS"].ToString();
                if (online.IsOnline(u.UserID) > 0)
                {
                    u.Online = true;
                }
                else
                {
                    u.Online = false;
                }
                lst.Users.Add(u);
            }
            return lst;


        }

        public System.Data.DataSet UsersByCriteria(Criteria c)
        {
            if (c.All)
            {
                ContactSearch dsMio = new ContactSearch();
                ObjectFactory.GetUserSearchTable().Fill(dsMio.UserSearch);
                DataSet dsRight = new DataSet();
                dsRight.Merge(dsMio);
                return dsRight;
            }
            return null;
        }

        public string AddContact(long idContact,string group)
        {
            
                WSIMProfilesTableAdapters.wsim_user_listTableAdapter userList = ObjectFactory.GetUserListTable();
                WSIMProfilesTableAdapters.wsim_userTableAdapter user = ObjectFactory.GetUserTable();

                long idUser = (long)user.GetID(OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name);

                userList.Insert(idUser, group, idContact);

                return ErrorCodesEnum.W2001_CONTACT_ADDED_SUCCESSFUL.ToString();
            
        }

        public string DeleteContact(long idContact)
        {
            WSIMProfilesTableAdapters.wsim_user_listTableAdapter userList = ObjectFactory.GetUserListTable();
            WSIMProfilesTableAdapters.wsim_userTableAdapter user = ObjectFactory.GetUserTable();

            long idUser = (long)user.GetID(OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name);

            userList.DeleteContact(idUser, idContact);

            return ErrorCodesEnum.W2002_CONTACT_DELETE_SUCCESSFUL.ToString();
        }

        #endregion
    }
}
